<?php
/*
 * ebiz front
 *
 * CategoryView
 *
 * @author allen
 *
 */

require_once '../app/util/TemplateReplaceUtil.php';
require_once '../app/util/TagUtil.php';
require_once '../app/views/HeaderView.php';
require_once '../app/views/FooterView.php';
require_once '../app/util/CommonUtil.php';
require_once '../app/services/CategoryService.php';

class CategoryFirstView extends BaseView{

//    public $leftMenus = array();
//    public $saleProductsGrop = array();
//    public $otherProductsGrop1 = array();
//    public $otherProductsGrop2 = array();
    private $params = array();
    
    private $categoryService;
    
    public function __construct(){
    	parent::__construct();
    	$this->categoryService = new CategoryService();
    }

	public function getCategoryFirstViewPage($params){
		$this->params = $params;
        $arr_navsetting = array(
            array('url'=>'','text'=>$this->leftMenus['fullname'])
        );
        $this->setPageTitle($this->params['category']['short_name']);
        $this->addCSS('productlist.css');
        $this->addJs('class1.js');
        //load templete
        $scpPath = "layout/category/" . $this->params['category']['url_title'] .".scp";
        $dataScp = TemplateReplaceUtil::getXmlTemplate($scpPath);
        if(empty($dataScp)){
        	HttpUtil::renderError();
        }
        //
        $header = '';
        $str_result = $header
                    . $this->getNavigation($arr_navsetting)
    		        . "<div class='p1_main'>"
    		        . $this->getContentLeftBlock($dataScp->left)
    		        . $this->getContentRigthBlock($dataScp->right)
    		        . "</div>";
    		        
        return $this->getHtmlPage($str_result);
	}
	
	public function getNavigation(){
		$str = '<div class="p1_loc"><span><a href="/">'. WEBSITEINDEXPAGETXT .'</a></span>><span><a href="#">'.$this->params['category']['short_name'].'</a></span></div>';
	    return $str;
	}

	public function getLeftTopBlock($left){
		$str = '<div class="p1_lefttop"><img src="'. ConfigConstants::getTImagePath() .'/p1_lefttopbg.gif" alt="" /></div>';
		return $str;
	}
	
	public function getContentLeftBlock($leftXml){
        $leftHtml = "<div class='p1_left'>" . $this->getCategoryVerticlMenu($leftXml) . '</div>';
        return $leftHtml;
	}

	/**
	 * @return string
	 */
	public function getCategoryVerticlMenu($leftXml){
		if(empty($leftXml) || empty($leftXml->item)){
			return "";
		}
		$items = $leftXml->item;
		$html= '<div class="p1_lefttop"><img src="'. ConfigConstants::getTImagePath() .'/p1_lefttopbg.gif" alt="" /></div>'
		       . "<div class='p1_leftcenter'>"
               . '<div class="p1_left01">
				       <h5>'. $this->params['category']['short_name'] .'</h5>
				       <div class="p1_left02top"></div>
				       <div class="p1_left03">';
               

         $i = 1;
		 foreach($items as $item){
		 	$html .=  '<ul>';   
		 	$html .=  '<li id="p1_li_'.$i.'" class="p1_left03_li">';
		 	$html .=  '<a href="#" class="menu2" id="'.$i.'" onclick="javascript:changeDivStatus(\''.$i.'\');">'.$item->title.'</a>';
		 	$html .=  '<ul id="p1_ul_'.$i.'">';
		 	foreach($item->metadata as $metadata){
		 		$html .= '<li><a href="/c/search'.$metadata->key.'">'.$metadata->title.'</a></li>';
		 	}		 	 
		 	$html .=  '</ul>';
		 	$html .=  '</li>';
		 	$html .=  '</ul>';
		 	$i++;
		 }		         
		 $html	.=	    '</div>
				       <div class="p1_left02bottom"></div>
				 </div>'
		       . "</div>"
		       . '<div class="p1_leftbottom"><img src="'.ConfigConstants::getTImagePath().'/p1_leftbottombg.gif" alt="" />
		                <div style="margin-top:10px;">
		       			<script type="text/javascript">
							<!--
				            google_ad_client = "pub-1231013902088237";
				            /* 200x200, 创建于 09-11-9 */
				            google_ad_slot = "0725208146";
				            google_ad_width = 200;
				            google_ad_height = 200;
				            //-->
			            </script>
			            <script type="text/javascript" src=" http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
			            </div> 
		         </div>';
		       
		  return $html;     
	}
	
	/**
	 * Get right block
	 *
	 * @param array $arr_param
	 * @return string
	 */
	public function getContentRigthBlock($rightXml){

		return "<div class='p1_right'>"
		       .$this->getRightTopBlack($rightXml->top)
		       .$this->getRightCenterBlock($rightXml->center)
		       .$this->getRightBottomBlock($rightXml->bottom)
               . '</div>';
	}
	
	public function getRightTopBlack($topXml){
	     if(empty($topXml) || empty($topXml->item)){
	          return "";
	     } 
	     $html = '<div class="p1_right01">';
	     $html .= '<div class="p1_right01top"></div>';
	     $html .= '<div class="p1_right01title">';
	     $html .= '<div class="p1_right01title_01">';
	     $html .= '<img width="86" height="22" alt="" src="'. ConfigConstants::getTImagePath() .'/category/xscx.gif"/>';
	     $html .= '</div>';
	     $html .= '</div>';
	     $html .= '<div class="p1_right01_1">';
         foreach($topXml->item as $item){
         	 $imagePath = ConfigConstants::getTImagePath() . "/category/" . $item->image;
         	 $type = $item->type;
         	 $productItems = $this->categoryService->getSpecialProductByType($type);
             $i = 1;
             foreach($productItems as $productItem){

             	if($i <= 9){
             		$urlTitle = $productItem['url_title'];
             		$title = CommonUtil::cut_str($productItem['title'],23);
             		$marketPrice = $productItem['marketing_price'];
             		$salePrice = $productItem['sale_price'];
             		$ifolder = $productItem['ifolder'];
             		$iname = $productItem['iname'];
             		if(empty($iname)){
             			$productImage250 = ConfigConstants::getDefaultProductImagePath("250");
             			$productImage100 = ConfigConstants::getDefaultProductImagePath("100");
             		}else{
             			$productImage250 = ConfigConstants::getProductImagePath($ifolder,$iname,"250");
             			$productImage100 = ConfigConstants::getProductImagePath($ifolder,$iname,"100");
             		}
              		if($i == 1){
             			$html .= '<div class="p1_right01_2">';
             			$html .= '<a href="/p/'. urlencode($urlTitle) .'/'. $productItem['id'] .'">';
             			$html .= '<img src="'.$productImage250.'" alt="" />';
             			$html .= '</a>';
             			$html .= '<div class="p1_right01_2p">';
             			$html .= '<div class="p1_right01_2p1">';
             			$html .= '<p>'.$title.'</p>';
             			$html .= '<p>米米价|<span class="p1_mprice">￥' . $marketPrice . '</span></p>';
             			$html .= '<p>促销价|<span class="p1_mprice1">￥' . $salePrice . '</span></p>';
             			$html .= '</div>'; 
             			$html .= '</div>'; 
             			$html .= '</div>'; 
             		}else{
                        $html .= '<div class="p1_right01_3" style="height:auto;">';
                        $html .= '<a href="/p/'. urlencode($urlTitle) .'/'. $productItem['id'] .'">';
                        $html .= '<img src="'.$productImage100.'" alt="" />';
                        $html .= '</a>';
                        $html .= '<div class="p1_right01_3p p1_right01_3p'.$i.'" style="height:auto">';
                        $html .= '<div style="height:60px;padding-left:25px;dispaly:block;word-break:break-all;width:80px;">'.$title.'</div>';
                        $html .= '<div>';
                        $html .= '<p>米米价|<span class="p1_mprice">￥' . $marketPrice . '</span></p>';
                        $html .= '<p>促销价|<span class="p1_mprice1">￥' . $salePrice . '</span></p>'; 
                        $html .= '</div>';
                        $html .= '</div>';                      
                        $html .= '</div>';
             		}
             	}
             	$i++;
             }   
         }
        $html .= '</div>'; 
        $html .= '</div>'; 
        return $html;
	}
	
	public function getRightCenterBlock($centerXml){
	     if(empty($centerXml) || empty($centerXml->item)){
	            return "";
	     } 	     
	     $html = '';
	     foreach($centerXml->item as $item){
	          $imagePath = ConfigConstants::getTImagePath() . "/category/" . $item->image;
	          $type = $item->type;
	          $productItems = $this->categoryService->getSpecialProductByType($type);
	          if(empty($productItems)){
	          	 continue;
	          }
	          $html .= '<div class="p1_right02">';  
              $html .= '<div class="p1_right02title">';
              $html .= '<div class="p1_right02title_01">';
              $html .= '<img src="'.$imagePath.'" width="86" height="22" alt="" />';
              $html .= '</div>';     
              $html .= '</div>';
              $i = 1;
	          foreach($productItems as $productItem){
	          	  if($i <= 6){
		          	    $title = CommonUtil::cut_str($productItem['title'],25);
		          	    $urlTitle = $productItem['url_title'];
	                    $marketPrice = $productItem['marketing_price'];
	                    $salePrice = $productItem['sale_price'];
	                    $ifolder = $productItem['ifolder'];
	                    $iname = $productItem['iname'];
	                    if(empty($iname)){
	                        $productImage100 = ConfigConstants::getDefaultProductImagePath("100");
	                    }else{
	                        $productImage100 = ConfigConstants::getProductImagePath($ifolder,$iname,"100");
	                    }	      
	                    if($i == 1){
	                    	$html .= '<div class="p1_right02_01">';
	                    }else{
	                    	$html .= '<div class="p1_right02_01 p1_right02_02">';
	                    }    	  	
	                    $html .= '<a href="/p/'. urlencode($urlTitle) .'/'. $productItem['id'] .'">';
		                $html .= '<img src="'.$productImage100.'" alt="" />';
		                $html .= '</a>';
		                $html .= '<p style="height:60px;">'; 
		                $html .= $title;
		                $html .= '</p>'; 
		                $html .= '<p><span>￥'.$salePrice.'</span></p>';
		                $html .= '</div>';	          	  	
	          	  }
                  $i++;
	          } 
              $html .= '</div>'; 
	     }

	     return $html;	
	}

    public function getRightBottomBlock($bottomXml){
     if(empty($bottomXml) || empty($bottomXml->item)){
            return "";
     }    	
    	$html = '<div class="p1_right03">
     <div class="p1_right03top"></div>
     <div class="p1_right03title"><img src="'.ConfigConstants::getTImagePath().'/p1_righttitleimg4.gif" width="147" height="19" alt="" /></div>
     <div class="p1_right03_1">';
     foreach($bottomXml->item as $item){
     	$html .= '<ul class="p1_right03_1ul">';
     	$html .= '<li class="seperator">'.$item->title.'</li>';
     	$i = 0;
     	foreach($item->metadata as $metadata){
     		if($i == 0){
     			$html .= '<li class="p1_right03li01" onclick="javascript:window.location=\'/c/search'.$metadata->key.'\';">'.$metadata->title.'</li>';
     		}else{
     			$html .= '<li class="p1_right03li" onclick="javascript:window.location=\'/c/search'.$metadata->key.'\';">'.$metadata->title.'</li>';
     		}
     		$i++;
     	}
     	$html .= '</ul>';
     }
     $html .= '</div>
     </div>';
    	
    	return $html;
    }

}




